import requests
from urllib.parse import quote
from lxml import etree
import json
import pandas as pd
from os import listdir


def func(x):
    if x == "文科":
        return '2'
    elif x == "理科":
        return '1'
    return '3'


def get_infos(s_v, a_v):
    List = []
    for p in range(1, 100):
        url = f'https://api.eol.cn/gkcx/api/?access_token=&local_province_id={a_v}&page={p}&school_id={s_v}&signsafe=&size=30&special_group=&uri=apidata/api/gkv3/plan/school&year=2020'
        lua = '''function main(splash, args)
          splash.private_mode_enabled = false
          assert(splash:go("''' + url + '''"))
          assert(splash:wait(0.5))
          splash:set_user_agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36")
          return {
            html = splash:html(),
          }
        end'''
        response = requests.get("http://localhost:7000/execute?lua_source=" + quote(lua))
        data = json.loads(response.text)["html"]
        html = etree.HTML(data)
        data = json.loads(html.xpath('/html/body/pre/text()')[0])
        if len(data['data']['item']) > 0:
            List += data['data']['item']
        else:
            break
    if len(List) != 0:
        data = pd.DataFrame(List)
        data['num'] = data['num'].apply(str)
        data['school_id'] = data['school_id'].apply(str)
        data['sg_type'] = data['sg_type'].apply(str)
        data['sp_type'] = data['sp_type'].apply(str)
        data['special_group'] = data['special_group'].apply(str)
        data['year'] = data['year'].apply(str)
        data["local_type_name"] = data["local_type_name"].apply(func)
        dic = {}
        type_1 = []
        for i in range(len(data.loc[data['local_type_name'] == '1'])):
            d = data.loc[data['local_type_name'] == '1'].iloc[i]
            type_1.append(d.to_dict())
        dic['1'] = type_1
        type_2 = []
        for i in range(len(data.loc[data['local_type_name'] == '2'])):
            d = data.loc[data['local_type_name'] == '2'].iloc[i]
            type_2.append(d.to_dict())
        dic['2'] = type_2
        type_3 = []
        for i in range(len(data.loc[data['local_type_name'] == '3'])):
            d = data.loc[data['local_type_name'] == '3'].iloc[i]
            type_3.append(d.to_dict())
        dic['3'] = type_3
        return dic
    else:
        return 0


if __name__ == '__main__':
    with open('school_name_id.json', 'r', encoding='utf-8') as f:
        school_id = f.read()
        school_id = json.loads(school_id)
    with open('地区_id.json', 'r', encoding='utf-8') as f:
        area_id = f.read()
        area_id = json.loads(area_id)
    for s_k, s_v in school_id.items():
        Map = {}
        if f'{s_k}.json' not in listdir('F:/高校分析与推荐系统/data/大学招生情况/'):
            dic = {}
            for a_k, a_v in area_id.items():
                d = get_infos(s_v, a_v)
                if d != 0:
                    dic[a_k] = d
                print(f'{s_k}  {a_k}   finish')
            Map[s_k] = dic
            Map['school_name'] = s_k

            with open(f'F:/高校分析与推荐系统/data/大学招生情况/{s_k}.json', 'w', encoding='utf-8') as f:
                json.dump(Map, f, indent=4, ensure_ascii=False)
